package com.nlp.visualization.utils;


import com.alibaba.fastjson.JSONReader;
import com.nlp.visualization.pojo.NLP.weibo.WeiboCSVEntity;
import org.apache.commons.io.FileUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;

public class MyFIleUtil {

    public static void writeQueue2File(ConcurrentLinkedQueue<String> queue, File outputFile) throws IOException {

        int size = queue.size();
        int i = 0;
        String buf = "";
        for (String str : queue) {
            if (i % 4096 == 0) {
                FileUtils.write(outputFile, buf, true);
                buf = "";
            } else {
                buf += str + "\n";
            }
            i++;
        }
        FileUtils.write(outputFile, buf, true);

    }

    public static List parseBigJson(File in) {
        JSONReader reader = null;
        List<WeiboCSVEntity> csvEntities = new ArrayList<>();
        try {
            reader = new JSONReader(new FileReader(in.getAbsolutePath()));
            reader.startArray();
            while (reader.hasNext()) {
//                String key = reader.readString();
                WeiboCSVEntity vo = reader.readObject(WeiboCSVEntity.class);
                csvEntities.add(vo);
            }
            reader.endArray();
            reader.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return csvEntities;

    }


}
